package org.ptkparty.server.service;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.ptkparty.shared.model.UserEntity;

public class MailService {

	private static final Logger LOGGER = Logger.getLogger(MailService.class.getName());

	private static MailService instance;

	public static synchronized MailService getInstance() {
		if (instance == null) {
			instance = new MailService();
		}
		return instance;
	}

	public void sendWelcomeEmail(UserEntity userEntity) throws Exception {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("guillaume.prime@gmail.com"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(userEntity.getEmail(), userEntity.getNickname()));
			msg.setSubject("Bienvenue sur PTKarty");
			msg.setText("");
			Transport.send(msg);

		} catch (Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw e;
		}
	}
}
